﻿using System;
using Microsoft.Office.Tools.Ribbon;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Smrf.NodeXL.Core;
using pptGraphCreatorAddIn.Skins;

namespace pptGraphCreatorAddIn
{
    public partial class Ribbon1
    {
        PowerPoint.Presentation pre = null;

        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void CreateGraphButton_Click(object sender, RibbonControlEventArgs e)
        {
            PowerPoint.Shapes shaps = CreateNewSlide();
            int NbNodes = int.Parse(NbNodesEditBox.Text);

            string layout = (string.IsNullOrEmpty(comboLayout.Text) ? "Circle" : comboLayout.Text);
            IGraph G = OfficeHelper.CreateGraph(NbNodes, pre.PageSetup.SlideWidth - 20, pre.PageSetup.SlideHeight - 20, layout);

            float NodeSize = 20;
            if (!float.TryParse(NodeSizeEditBox.Text, out NodeSize))
            {
                if (G.Vertices.Count <= 10) //He just to not have Big node sizes
                {
                    NodeSize = 27;
                }
                else //He the node sizes will be proportional to graph size
                {
                    NodeSize = Math.Min(pre.PageSetup.SlideHeight / (2 * G.Vertices.Count),
                        pre.PageSetup.SlideWidth / (2 * G.Vertices.Count));
                }
            }

            DrawGraph(shaps, G, NodeSize);
        }

        private static void DrawGraph(PowerPoint.Shapes shaps, IGraph G, float NodeSize)
        {
            //First draw the vertices
            int id = 0;
            foreach (var v in G.Vertices)
            {
                PowerPoint.Shape sp = OfficeHelper.AddNode(NodeSize, v.Location.X, v.Location.Y, shaps);
                BlueSkin Blue = new BlueSkin();
                //GreenSkin Green = new GreenSkin();
                //Green.Apply(sp);
                Blue.Apply(sp);

                if (v.ContainsKey("name"))
                {
                    string txt = v.GetValue("name").ToString();
                    sp.TextEffect.Text = txt;
                    sp.TextEffect.Alignment = Microsoft.Office.Core.MsoTextEffectAlignment.msoTextEffectAlignmentStretchJustify;
                    sp.TextEffect.FontSize = NodeSize / 3;
                }

                v.Tag = sp;
                sp.Name = string.Format("Node_{0}", id);
                id++;
            }

            //Then Draw the edges
            foreach (var w in G.Edges)
            {
                var connector = shaps.AddConnector(Microsoft.Office.Core.MsoConnectorType.msoConnectorStraight, 0, 0, 100, 100);
                PowerPoint.Shape V1 = (PowerPoint.Shape)w.Vertex1.Tag;
                PowerPoint.Shape V2 = (PowerPoint.Shape)w.Vertex2.Tag;

                connector.ConnectorFormat.BeginConnect(V1, 1);
                connector.ConnectorFormat.EndConnect(V2, 1);
                connector.RerouteConnections();
            }
        }

        private PowerPoint.Shapes CreateNewSlide()
        {
            if (pre == null)
            {
                pre = Globals.ThisAddIn.Application.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue);
            }

            PowerPoint.CustomLayout Layout = pre.SlideMaster.CustomLayouts[7]; //Blank layout

            PowerPoint.Slide myslide = pre.Slides.AddSlide(pre.Slides.Count + 1, Layout);
            PowerPoint.Shapes shaps = myslide.Shapes;

            return shaps;
        }

        private void BtnLoadGraph_Click(object sender, RibbonControlEventArgs e)
        {
            String GraphPath = String.Empty;

            //We get the Graph file path
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                GraphPath = openFileDialog1.FileName;
            }

            IGraph G = null;
            try
            {
                G = OfficeHelper.LoadGraph(GraphPath);
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("Please check your file format", "File Format Error");
            }

            //Read the the graph.

            if (G == null)
            {
                return;
            }

            //Create a new slide
            PowerPoint.Shapes shapes = CreateNewSlide();

            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, (int)pre.PageSetup.SlideWidth - 20, (int)pre.PageSetup.SlideHeight - 20);

            GraphLayoutFactory.ApplyLayout(G, comboLayout.Text, rect);

            float NodeSize = 0;
            if (!float.TryParse(NodeSizeEditBox.Text, out NodeSize))
            {
                NodeSize = Math.Min(pre.PageSetup.SlideHeight / (2 * G.Vertices.Count),
                    pre.PageSetup.SlideWidth / (2 * G.Vertices.Count));
            }

            //Draw the graph on it            
            DrawGraph(shapes, G, NodeSize);

        }

        private void gallery1_Click(object sender, RibbonControlEventArgs e)
        {
            switch (gallery1.SelectedItem.Label)
            {
                case "BlueSkin":
                    OfficeHelper.ApplySkin(new BlueSkin());
                    break;
                case "GreenSkin":
                    OfficeHelper.ApplySkin(new GreenSkin());
                    break;
                case "RedSkin":
                    OfficeHelper.ApplySkin(new RedSkin());
                    break;
                case "BlackSkin":
                    OfficeHelper.ApplySkin(new BlackSkin());
                    break;
                default:
                    break;
            }
        }

        
    }
}
